package com.lihaogn.sell.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lihaogn.sell.entity.User;
import com.lihaogn.sell.mapper.OrderMapper;
import com.lihaogn.sell.mapper.UserMapper;
import com.lihaogn.sell.service.UserService;
import com.lihaogn.sell.vo.ListVo;
import com.lihaogn.sell.vo.SearchPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @FileName: UserServiceImpl
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-05-17 11:48
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public int getUserById(String userId) {
        return userMapper.queryUserById(userId);
    }

    @Override
    public ListVo searchUser(SearchPageVo searchPageVo) {

        Map<String, Object> queryMap = new HashMap<>();
        ListVo listVo = new ListVo();

        List<User> userList;
        int searchCount ;

        Integer limit = searchPageVo.getLimit();
        Integer page = (searchPageVo.getPage()-1)*limit;
        String username = searchPageVo.getName();
        String start = searchPageVo.getStart();
        String end = searchPageVo.getEnd();

        queryMap.put("page", page);
        queryMap.put("size", limit);

        if (!"".equals(username)) {
            // username不为空，按照username查找
            User user = userMapper.queryUserByName(username);
            userList = new ArrayList<>();
            userList.add(user);

            listVo.setCode(0);
            listVo.setMsg("success");
            listVo.setCount(1);
            listVo.setData(userList);

            return listVo;
        } else if ("".equals(start)) {
            // 开始日期为空，则查找结束日期前的
            queryMap.put("end", end);
            userList = userMapper.querytUserByDateForPage(queryMap);
            searchCount = userMapper.queryCountByDate(queryMap);
        } else if ("".equals(end)) {
            // 结束日期为空，则查找开始日期后的
            queryMap.put("start", start);
            userList = userMapper.querytUserByDateForPage(queryMap);
            searchCount = userMapper.queryCountByDate(queryMap);
        } else {
            // 查找开始日期到结束日期的
            queryMap.put("end", end);
            queryMap.put("start", start);
            userList = userMapper.querytUserByDateForPage(queryMap);
            searchCount = userMapper.queryCountByDate(queryMap);
        }

        listVo.setCode(0);
        listVo.setMsg("success");
        listVo.setCount(searchCount);
        listVo.setData(userList);

        return listVo;
    }

    @Override
    public int getUserRowsCount() {
        return userMapper.queryUserRowsCount();
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.queryAllUser();
    }

    @Override
    public List<User> getAllUserForPage(String page, String size) {

        Map<String, Integer> pageMap = new HashMap<>();

        int pageOne = Integer.valueOf(page);
        int pageSize = Integer.valueOf(size);
        int start = (pageOne - 1) * pageSize;

        pageMap.put("page", start);
        pageMap.put("size", pageSize);

        List<User> userList = userMapper.queryUserForPage(pageMap);

        return userList;
    }

    @Override
    public int addOneUser(User user) {
        return userMapper.insertOneUser(user);
    }

    @Override
    public int updateOneUser(User user) {
        return userMapper.updateOneUser(user);
    }

    @Override
    public int deleteOneUser(String userId) {

        orderMapper.deleteOrderDetailByUserId(userId);
        orderMapper.deleteOrderByUserId(userId);
        return userMapper.deleteOneUser(userId);
    }

    @Override
    public int deleteMultiUser(HttpServletRequest request) {

        // 记录删除的个数
        int num;
        BufferedReader reader;

        try {
            reader = request.getReader();

            StringBuffer res = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                res.append(line);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            ArrayList<String> idList = objectMapper.readValue(res.toString(), ArrayList.class);

            HashMap<String, Object> map = new HashMap<>();
            map.put("idList", idList);

            for (int i = 0; i < idList.size(); i++) {
                orderMapper.deleteOrderDetailByUserId(idList.get(i));
                orderMapper.deleteOrderByUserId(idList.get(i));
            }

            num = userMapper.deleteMultiUser(map);
            System.out.println("num:"+num+"idList.size:"+idList.size());
            if (num == idList.size()) {
                return 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
